<?php
namespace App\Service\Xcx\Ywmall;

use App\Service\Xcx\BaseService as Service;

class FuwuService extends Service
{
    //服务

    //列表
    public static function index($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwFuwu)->where('del', 0);
//        if ($pas['uid']) {
//            $us = self::dbConn(self::$dbUserShop)
//                ->where('del', 0)
//                ->where('uid', $pas['uid'])
//                ->get();
//            if (count($us)) {
//                $shopids = [];
//                foreach ($us as $v) {
//                    $shopids[] = $v->shop_id;
//                }
//                $db = $db->whereIn('shop_id', $shopids);
//            }
//        }
        if ($pas['uid']) {
                $db = $db->where('uid', $pas['uid']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据', [], $total);
        }
        $ary = [];
        foreach ($res as $v) {
            $shop_id = $v->shop_id;
            $shop_name = '小店名称';
            if ($shop_id) {
                $shopOne = self::dbConn(self::$dbYwShop)
                    ->where('del', 0)
                    ->where('id', $shop_id)
                    ->first();
                if ($shopOne) {
                    $shop_name = $shopOne->title;
                }
            }
            $ary[] = [
                'id' => $v->id, 'title' => $v->title,
                //'cover' => file_exists($v->cover) ? env('DOMAIN') . '/'. $v->cover : '',
                'cover' => $v->cover ? $v->cover : '',
                'code' => $v->code, 'price' => toNumber($v->price),
                'price_old' => toNumber($v->price_old),
            ];
        }
        return self::toback(0, '', $ary, $total);
    }

    //详情
    public static function onebyid($id)
    {
        $res = self::dbConn(self::$dbYwFuwu)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $codeOne = self::dbConn(self::$dbQr)
            ->where('code', $res->code)
            ->first();
        $res->title = $res->title ? $res->title : '-';
        $res->price = toNumber($res->price);
        $res->price_old = toNumber($res->price_old);
        //$res->cover = file_exists($res->cover) ? env('DOMAIN') . '/' . $res->cover : '';
        //浏览量+最近活跃
        $res->visit_total = self::dbConn(self::$dbQrlog)->where('code', $res->code)->count();
        $logOne = self::dbConn(self::$dbQrlog)
            ->where('code', $res->code)
            ->orderBy('id', 'desc')
            ->first();
        $res->lasttime = $logOne ? $logOne->addtime : '-';
        $res->show_code = $codeOne ? $codeOne->show_code : '-';
        $res->code = $res->code ? $res->code : '-';
        //属性集合
        $res->attrs = self::dbConn(self::$dbAttr)
            ->select('id', 'attr_name', 'attr_val')
            ->where('del', 0)
            ->where('yw_type', 3)
            ->where('yw_id', $id)
            ->get();
        return self::toback(0, '', $res);
    }

    //获取码
    public static function oneidByCode($code)
    {
        $gOne = self::dbConn(self::$dbYwGoods)
            ->where('code', $code)
            ->first();
        if (empty($gOne)) {
            return self::toback(-2, '没有商品');
        }
        return self::toback(0, '', $gOne->id);
    }

    //获取码
    public static function codeBySerial($serial)
    {
        $qrOne = self::dbConn(self::$dbQr)
            ->where('show_code', $serial)
            ->first();
        if (empty($qrOne)) {
            return self::toback(-2, '没有码');
        }
        if (!$qrOne->yw_id) {
            return self::toback(-2, '码没被使用');
        }
        $resBack = [
            'code' => $qrOne->code,
            'yw_type' => $qrOne->yw_type,
            'yw_id' => $qrOne->yw_id,
        ];
        return self::toback(0, '', $resBack);
    }

    //新增
    public static function add($pas)
    {
//        $pas['shequ_id'] = 0;
//        if ($shequ) {
//            $sqOne = self::dbConn(self::$dbShequ)
//                ->where('del', 0)
//                ->where('title', $shequ)
//                ->first();
//            if (empty($sqOne)) {
//                return self::toback(-2, '没有社区');
//            }
//            $pas['shequ_id'] = $sqOne->id;
//        }
//        $codeOne = self::dbConn(self::$dbQr)
//            ->where('code', $pas['code'])
//            ->first();
//        if (empty($codeOne)) {
//            return self::toback(-2, '没有码');
//        }
//        if ($codeOne->yw_id) {
//            return self::toback(-2, '已被使用');
//        }
        $pas['addtime'] = $pas['uptime'] = toDate();
        $lastid = self::dbConn(self::$dbYwFuwu)->insertGetId($pas);
        if ($pas['code']) {
            self::dbConn(self::$dbQr)
                ->where('code', $pas['code'])
                ->update(['yw_type' => 1, 'yw_id' => $lastid, 'usetime' => toDate()]);
        }
        if ($pas['cover']) {
            self::dbConn(self::$dbImgTmp)
                ->where('link', $pas['cover'])
                ->update(['usetime' => toDate()]);
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $fwOne = self::dbConn(self::$dbYwFuwu)->where('id', $id)->first();
        if (empty($fwOne)) {
            return self::toback(-2, '没有数据');
        }
        $pas['uptime'] = toDate();
        self::dbConn(self::$dbYwFuwu)
            ->where('id', $id)
            ->update($pas);
        if ($pas['cover']) {
            self::dbConn(self::$dbImgTmp)
                ->where('link', $pas['cover'])
                ->update(['usetime' => toDate()]);
        }
        return self::toback();
    }

    //修改图片
    public static function imgUpload($id, $img)
    {
        $fwOne = self::dbConn(self::$dbYwFuwu)->where('id', $id)->first();
        if (empty($fwOne)) {
            return self::toback(-2, '没有数据');
        }
        if (file_exists($fwOne->thumb)) {
            unlink($fwOne->thumb);
        }
        self::dbConn(self::$dbYwFuwu)->where('id', $d)->update(['thumb' => $img]);
        return self::toback();
    }

//    //分类获取
//    public static function cateList()
//    {
//        $ary = [];
//        $res = self::dbConn(self::$dbCate)
//            ->where('del', 0)
//            ->where('type', 3)
//            //->where('pid', '>', 0)
//            ->get();
//        if (!count($res)) {
//            return self::toback(0, '', $ary);
//        }
//        foreach ($res as $v) {
//            $pid = $v->pid;
//            $cate_name = $v->title;
//            if ($pid) {
//                $pone = self::dbConn(self::$dbCate)
//                    ->where('pid', $pid)
//                    ->first();
//                $cate_name = $pone ? $pone->title . '-' . $cate_name : $cate_name;
//            }
//            $ary[] = [
//                'id' => $v->id, 'title' => $cate_name,
//            ];
//        }
//        return self::toback(0, '', $ary);
//    }
//
//    //分类设置
//    public static function cateEdit($fwid, $cateid)
//    {
//        self::dbConn(self::$dbYwFuwu)
//            ->where('id', $fwid)
//            ->update(['cate_id' => $cateid]);
//        $cateOne = self::dbConn(self::$dbCate)
//            ->where('id', $cateid)
//            ->first();
//        $cate_name = $cateOne->title;
//        if ($cateOne->pid) {
//            $pOne = self::dbConn(self::$dbCate)
//                ->where('id', $cateOne->pid)
//                ->first();
//            $cate_name = $pOne->title . '-' . $cate_name;
//        }
//        $arys = [
//            'id' => $cateid, 'title' => $cate_name,
//        ];
//        return self::toback(0, '', $arys);
//    }

    //属性获取
    public static function attrList($cate_id, $id)
    {
        $cateOne = self::dbConn(self::$dbAttr)
            ->where('pid', $cate_id)
            ->get();
//        if (!$cateOne) {
//            return self::toback(-2, '没有属性');
//        }
        $tmpOne = self::dbConn(self::$dbYwFuwu)
            ->where('id', $id)
            ->first();
        if (!$tmpOne) {
            return self::toback(-2, '没有个人码');
        }
        $arys = [];
        $attrTmps = self::dbConn(self::$dbAttr)
            ->where('del', 0)
            ->where('yw_type', 1)
            ->where('yw_id', $id)
            ->get();
        if (!count($attrTmps)) {
            $attrs = ($cateOne&&$cateOne->attr_str) ? explode(',', $cateOne->attr_str) : [];
            foreach ($attrs as $v) {
                $arys[] = [
                    'id' => 0, 'key' => $v, 'val' => '',
                ];
            }
        } else {
            foreach ($attrTmps as $v) {
                $arys[] = [
                    'id' => $v->id, 'key' => $v->attr_name, 'val' => $v->attr_val,
                ];
            }
        }
        if (!count($arys)) {
            return self::toback(-2, '没有属性');
        }
        self::dbConn(self::$dbYwFuwu)
            ->where('id', $id)
            ->update(['cate_id' => $cate_id]);
        return self::toback(0, '', $arys);
    }

    //属性新增
    public static function attrAdd($fwid, $key, $val)
    {
        $attrOne = self::dbConn(self::$dbAttr)
            ->where('del', 0)
            ->where('yw_id', $fwid)
            ->where('attr_name', $key)
            ->first();
        if (!empty($attrOne)) {
            return self::toback(-2, '属性已存在');
        }
        $lastid = self::dbConn(self::$dbAttr)->insertGetId([
            'yw_type' => 1, 'yw_id' => $fwid,
            'attr_name' => $key, 'attr_val' => $val,
            'addtime' => toDate(), 'uptime' => toDate(),
        ]);
        if (!$lastid) {
            return self::toback(-2, 'error');
        }
        return self::toback();
    }

    //属性修改
    public static function attrEdit($attrid, $key, $val)
    {
        $attrOne = self::dbConn(self::$dbAttr)
            ->where('del', 0)
            ->where('id', $attrid)
            ->first();
        if (empty($attrOne)) {
            return self::toback(-2, '属性不存在');
        }
        $up = self::dbConn(self::$dbAttr)->where('id', $attrid)
            ->update([
                'attr_name' => $key, 'attr_val' => $val, 'uptime' => toDate(),
            ]);
        if (!$up) {
            return self::toback(-2, 'error');
        }
        return self::toback();
    }

    //属性删除
    public static function attrDel($attrid)
    {
        $attrOne = self::dbConn(self::$dbAttr)
            ->where('del', 0)
            ->where('id', $attrid)
            ->first();
        if (empty($attrOne)) {
            return self::toback(-2, '属性不存在');
        }
        $up = self::dbConn(self::$dbAttr)->where('id', $attrid)
            ->update([
                'del' => 1, 'deltime' => toDate(),
            ]);
        if (!$up) {
            return self::toback(-2, 'error');
        }
        return self::toback();
    }
}